package Main;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.media.opengl.GL;
import javax.swing.JOptionPane;

import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;


public class ItemLevel {
	Texture m_Level;
	Point3D m_tranlevel;
	Point3D m_PosLevel;
	public ItemLevel()
	{
		
	}
	public void Init(int _level)
	{
		m_Level=LoadTexture("level"+_level+".png");
		m_PosLevel=new Point3D(0,0,0);
		m_tranlevel=new Point3D(-55,0,0);
	}
	public void Display(GL gl,Level _level)
	{
		gl.glPushMatrix();
		m_Level.bind();
		gl.glTranslated(m_tranlevel.X, m_tranlevel.Y, m_tranlevel.Z);
		if(m_tranlevel.X>=-55&&m_tranlevel.X<=-15)
		{
			m_tranlevel.X+=1;
		}
		else
		if(m_tranlevel.X>=0&&m_tranlevel.X<=20)
		{
			m_tranlevel.X+=1;
		}else
			m_tranlevel.X+=0.2;
		if(m_tranlevel.X>=20)
		{
			_level.m_testStart=true;
		}
		gl.glBegin(GL.GL_QUADS);
        // Front Face
        gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f( 0, 0, 0);
        gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 20f, 0, 0);
        gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 20f, 4f, 0);
        gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f( 0, 4f, 0);
        gl.glEnd();
        gl.glPopMatrix();
	}
	public Texture LoadTexture(String _path){
		Texture _result;
		BufferedImage image;
		try{
			image = ImageIO.read(this.getClass().getResource(_path));
			_result = TextureIO.newTexture(image,false);
		}catch(IllegalArgumentException e){
			JOptionPane.showMessageDialog(null, "Can not load file \"" + _path + "\". The application will be exit !"+e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
			//System.exit(0);
			return null;
		}catch(IOException e){
			e.printStackTrace();
			return null;
		}
		return _result;
		
	}
}
